<?php
class Zendvn_Model_Table_Language extends Zend_Db_Table{
	protected $_name = "languages";
	protected $_primary = "id"; 
	
	public $_searchFields = array('name'=>'- Name field','code'=>'- Code field');
	
	protected $_listSQL;
	
	public function itemInSelect($arrParam = null,$options = null){
		$db = $this->getAdapter();
		
		$select = $db->select()->from('user_group',array('id','name'));
		
		$result = $db->fetchPairs($select);
		
		$result[0] = '--Select a group--';
		ksort($result);
		
		return $result;
	}
	
	
	
	public function sortItem($arrParam = null,$options = null){
		if($options == null){
			
			foreach ($arrParam['cid'] as $val){				
				$where = 'id = ' . $val;
				$data = array('ordering'=>$arrParam['ordering'][$val]);
				$this->update($data, $where);
				
			}
		}
	}
	
	public function defaultLang($arrParam = null,$options = null){
		if($options  == null){
			
			$default = ($arrParam['s'] == 0 )? 1:0;	
			$itemInfo = $this->getItem(array('id'=>(int)$arrParam['id']));
			$area = $itemInfo['area'];
			if($default == 1){
				$data = array('default_lang' => 0);
				$where = "area ='" . $area . "'";
				$this->update($data, $where);
				
				$data = array('default_lang' =>1,'status'=>'active');
				$where = "area ='" . $area . "' AND id = " . (int)$arrParam['id'];
				$this->update($data, $where);
			}
		}
	}
	
	
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';	
			
			$itemInfo = $this->getItem(array('id'=>(int)$arrParam['id']));
		
			if($itemInfo['default_lang'] != 1){											
				$data = array('status'=>$status);
				$where = 'id = ' . (int)$arrParam['id'];
				$this->update($data, $where);
			}
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){
						$itemInfo = $this->getItem(array('id'=>(int)$val));
						
						if($itemInfo['default_lang'] !=1 ){
							$where = 'id = ' . $val;
							$data = array('status'=>$status);
							$this->update($data, $where);
						}
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		if($options == null){
			if((int)$arrParam['id']>0){
				$itemInfo = $this->getItem(array('id'=>$arrParam['id']));				
				if($itemInfo['delete'] == 'allow' && $itemInfo['default_lang'] == 0){					
					$where = 'id = ' . (int)$arrParam['id'];
					$this->delete($where);
				}
			}
		}
		if($options['task'] == 'multi'){		
			if(count($arrParam['cid'])>0){	
				foreach ($arrParam['cid'] as $val){
					$itemInfo = $this->getItem(array('id'=>(int)$val));						
					if($itemInfo['delete'] == 'allow' && $itemInfo['default_lang'] == 0){
						$where = 'id = ' . (int)$val;
						$this->delete($where);
					}
				}
			}
		}
	}
	
	public function getItem($arrParam = null,$options = null){
		/*echo '<br>' . __METHOD__;
		echo '<pre>';
		print_r($arrParam);
		echo '</pre>';*/
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){
	
		$db = $this->getAdapter();
		$select = $db->select()
					 ->from('languages',array('id'))
					 ->columns(array('name','code','area','default_lang','delete','status','ordering'));
		
		$arrFilter = $arrParam['arrFilter'];
		
		if(!empty($arrFilter['col'])){
			$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
		}

		if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
			$select->where('status = ?',$arrFilter['status']);
		}
		
		if(!empty($arrFilter['area']) && $arrFilter['area'] != 'none'){
			$select->where('area = ?',$arrFilter['area']);
		}
		
		if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
			$keyword = '%' . $arrFilter['keyword'] . '%';
			$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
		}
		
		$this->_listSQL = $select;
		
		$panigator = $arrParam['panigator'];	
		$select->limitPage($panigator['page'], $panigator['rowCount']);
		
		$result = $db->fetchAll($select);		 
		
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		$db = $this->getAdapter();
		$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
		$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
		$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
		$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
		$select->columns('COUNT(id) AS total');	
		//echo '<br>' . $select;
		$result = $db->fetchOne($select);
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		$this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null){
		
			$row = $this->fetchNew();
			$row->code			= $arrParam['code'];
			$row->name 			= $arrParam['name'];
			$row->area			= $arrParam['area'];			
			$row->status		= $arrParam['status'];
			$row->ordering		= $arrParam['ordering'];
			$row->save();
	}
	
	public function adminEdit($arrParam = null){
			$where  = 'id = ' . (int)$arrParam['id'];
			$row = $this->fetchRow($where);
			$row->code			= $arrParam['code'];
			$row->name 			= $arrParam['name'];
			$row->area			= $arrParam['area'];			
			$row->status		= $arrParam['status'];
			$row->ordering		= $arrParam['ordering'];
			$row->save();
	}
}